Code-Parsing Regular Expressions via Lighter.js

By  on  

Perfecting a regular expression can take a lot of time and testing but once achieved can be a absolutely golden. While looking through the source code of MooTools syntax highlighter Lighter.js I stumbled upon a few code-parsing regular expressions that you might be interested in.

The JavaScript

	// Matches a C style single-line comment.
	slashComments: /(?:^|[^\\])\/\/.*$/gm,
	
	// Matches a Perl style single-line comment.
	poundComments: /#.*$/gm,
	
	// Matches a C style multi-line comment.
	multiComments: /\/\*[\s\S]*?\*\//gm,
	
	// Matches a string enclosed by single quotes.
	aposStrings:   /'[^'\\]*(?:\\.[^'\\]*)*'/gm, 
	
	// Matches a string enclosed by double quotes.
	quotedStrings: /"[^"\\]*(?:\\.[^"\\]*)*"/gm, 
	
	// Matches both.
	strings:       /'[^'\\]*(?:\\.[^'\\]*)*'|"[^"\\]*(?:\\.[^"\\]*)*"/gm,
	
	// Matches a property: .property style.
	properties:    /\.([\w]+)\s*/gi,   
	
	// Matches a method call: .methodName() style.
	methodCalls:   /\.([\w]+)\s*\(/gm, 
	
	// Matches a function call: functionName() style.
	functionCalls: /\b([\w]+)\s*\(/gm,   
	
	// Matches any of the common brackets.
	brackets:      /\{|\}|\(|\)|\[|\]/g, 
	
	// Matches integers, decimals, hexadecimals.
	numbers:       /\b((?:(\d+)?\.)?[0-9]+|0x[0-9A-F]+)\b/gi 

Regular expressions can look heinous so I apologize to anyone whose brains imploded after looking at the above hieroglyphics text. Have useful regular expressions you use often? Share them!

Recent Features

  • By
    How I Stopped WordPress Comment Spam

    I love almost every part of being a tech blogger:  learning, preaching, bantering, researching.  The one part about blogging that I absolutely loathe:  dealing with SPAM comments.  For the past two years, my blog has registered 8,000+ SPAM comments per day.  PER DAY.  Bloating my database...

  • By
    6 Things You Didn’t Know About Firefox OS

    Firefox OS is all over the tech news and for good reason:  Mozilla's finally given web developers the platform that they need to create apps the way they've been creating them for years -- with CSS, HTML, and JavaScript.  Firefox OS has been rapidly improving...

Incredible Demos

  • By
    background-size Matters

    It's something that makes all men live in fear, and are often uncertain of. It's never spoken, but the curiosity is always there. Nine out of ten women agree in the affirmative. Advertisers do their best to make us feel inadequate but...

  • By
    Create a 3D Panorama Image with A-Frame

    In the five years I've been at Mozilla I've seen some awesome projects.  Some of them very popular, some of them very niche, but none of them has inspired me the way the MozVR team's work with WebVR and A-Frame project have. A-Frame is a community project...

Discussion

  1. David: very interesting, could you provide examples for each expression? ty so much!

  2. That’s pretty neat

    I’ve always used http://www.gskinner.com/RegExr/ to text my RegExp

  3. Well, im not a veteran programmer but i did a noobMatcher to test my regExp hehe

    http://vrs.host56.com/noobMatcher/

  4. Thanks for this Tutorial. Can u provide Demo for this.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!